package server

import (
	"go-test/src/com/miao/common/utils"
	"go-test/src/com/miao/server/context"
	"log"
)

func BindUser(userId string, channel *context.ChannelContext) {
	context.BindOne(channel.GroupContext.Users, channel, userId)
}

func BindGroup(groupId string, channel *context.ChannelContext) {
	context.BindOne(channel.GroupContext.Groups, channel, groupId)
}

func Send(packet interface{}, channel *context.ChannelContext) {
	if packet == nil {
		log.Println("packet is not allow empty")
		return
	}
	utils.SafeSend(channel.SendChan, packet)
}

func SendToUser(packet interface{}, groupContext *context.GroupContext, userId string) {
	if packet == nil {
		log.Println("packet is not allow empty")
		return
	}
	setWithUsers := context.FindSet(groupContext.Users, userId)
	if setWithUsers == nil {
		return
	}
	list := setWithUsers.List()
	for i := range list {
		channel := list[i].(*context.ChannelContext)
		//fmt.Println(channel.SendChan)
		utils.SafeSend(channel.SendChan,packet)
	}
}

func SendToGroup(packet interface{}, groupContext *context.GroupContext, groupId string) {
	if packet == nil {
		log.Println("packet is not allow empty")
		return
	}
}

